package com.haochang.chunkvod.controller.fragment.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.order.OrderBillAdapter;
import com.haochang.chunkvod.model.hostess.ConsumeInfo;
import com.haochang.chunkvod.model.hostess.GoodsFeeInfo;
import com.haochang.chunkvod.model.order.OrderData;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.internal.Utils;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author qintao
 *         created at 2016/6/13 10:38
 *         账单
 */

public class BillFragment extends BaseFragment
{
	private BaseTextView tv_person, tv_money_avg, tv_start_time, tv_time, tv_room_money, total_money;
	private BaseListView consume_info_list;
	private BaseTextView tv_info_bar;//消费明细的bar,	未获取数据时隐藏
	private BaseTextView tv_allowance_fee;//优惠金额
	private LinearLayout roomFeeLayout;//房费，桌台时隐藏
	private TitleView titleView;
	private ArrayList<GoodsFeeInfo> listInfos = new ArrayList<GoodsFeeInfo>();
	private String billId = "";
	private String roomNum = "";
	private OrderData orderData;
	private OrderBillAdapter adapter;
	private int type;//0房间，1桌台

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.activity_consume_info, container, false);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		initView();
		initData();
	}

	private void initView()
	{
		roomNum = getArguments().getString(IntentKey.ROOM_NUM);
		billId = getArguments().getString(IntentKey.BILL_ID);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(roomNum).setBackText("主页").setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				getActivity().finish();
			}

			@Override
			public void onDoneClick()
			{
				{
					List<String> list = new ArrayList<String>();
					list.add("退出登录");
					int location[] = new int[2];
					ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
					img.getLocationOnScreen(location);
					int x = location[0];
					final VodPopWindow pop = new VodPopWindow(getActivity(), list);
					pop.showPopupWindow(titleView, x);
					pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
					{
						@Override
						public void onSelectItem(int position)
						{
							switch (position)
							{
							case 0:
								((BaseActivity) getActivity()).loginOut();
								break;
							default:
								break;
							}
						}
					});
				}
			}
		});
		tv_person = (BaseTextView) findViewById(R.id.tv_person);
		tv_allowance_fee = (BaseTextView) findViewById(R.id.tv_allowance_fee);
		tv_money_avg = (BaseTextView) findViewById(R.id.tv_money_avg);
		tv_start_time = (BaseTextView) findViewById(R.id.tv_start_time);
		tv_time = (BaseTextView) findViewById(R.id.tv_time);
		tv_room_money = (BaseTextView) findViewById(R.id.tv_room_money);
		total_money = (BaseTextView) findViewById(R.id.total_money);
		consume_info_list = (BaseListView) findViewById(R.id.consume_info_list);
		tv_info_bar = (BaseTextView) findViewById(R.id.tv_info_bar);
		roomFeeLayout = (LinearLayout) findViewById(R.id.roomFeeLayout);
		tv_info_bar.setVisibility(View.GONE);
		type = getArguments().getInt(IntentKey.ROOM_TYPE);
		roomFeeLayout.setVisibility(type == 0 ? View.VISIBLE : View.GONE);
		adapter = new OrderBillAdapter(getActivity(), R.layout.item_consume_info_first, listInfos);
		consume_info_list.setAdapter(adapter);
	}

	private void initData()
	{
		orderData = new OrderData((BaseActivity) getActivity());
		requestConsumeInfo();
	}

	/**
	 * 获取账单具体一项 的详情
	 */
	private void requestConsumeInfo()
	{
		if (!TextUtils.isEmpty(billId))
			orderData.requestBillProfileInfo(billId, new OrderData.IBillProfileListener()
			{
				@Override
				public void onRequestSuccess(ConsumeInfo info)
				{
					if (info != null)
					{
						listInfos.clear();
						if (!CollectionUtils.isEmpty(info.getGoodsFeeInfos()))
						{
							tv_info_bar.setVisibility(View.VISIBLE);//显示消费明细的bar
							adapter.replaceAll(info.getGoodsFeeInfos());
						}
						tv_person.setText(String.valueOf(info.getCustomer_count()));
						tv_money_avg.setText(String.valueOf(SaleUtils.getSaleString(info.getEach_fee()) + "元"));
						tv_start_time.setText(TimeUtils.getMinutesString(info.getBegin_time()));
						tv_time.setText(TimeUtils.misToHour(info.getConsume_long()));
						tv_room_money.setText(String.valueOf(SaleUtils.getSaleString(info.getRoom_fee()) + "元"));
						total_money.setText(String.valueOf("¥" + SaleUtils.getSaleString(info.getTotalPrice())));
						//						tv_allowance_fee.setText(String.valueOf("优惠 ¥" + SaleUtils.getSaleString(info.getAllowance_fee()) + "元"));
					}
				}

				@Override
				public void onFail(int errno, String errMsg)
				{
					ToastUtils.showText(getActivity(), TextUtils.isEmpty(errMsg) ? "账单明细获取失败!" : errMsg);
				}
			});
	}
}
